package com.example.boxuegu;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class DBUtils {
    private static DBUtils instance =  null;
    private final SQLiteHeloer helper;
    private final SQLiteDatabase db;

    public DBUtils(Context context){
        helper = new SQLiteHeloer(context);
        db = helper.getWritableDatabase();
    }

    /*
     *
     * ??????
     * */
    public static DBUtils getInstance(Context context) {
        if (instance == null){
            instance = new DBUtils(context);
        }
        return instance;
    }

    /*
     *
     * 保存个人资料*/

    public void saveUserInfo(UserBean bean){
        ContentValues contentValues = new ContentValues();
        contentValues.put("userName",bean.userName);
        contentValues.put("nickName",bean.nikeName);
        contentValues.put("sex",bean.sex);
        contentValues.put("signature",bean.signatur);
        db.insert(SQLiteHeloer.U_USERINFO,null,contentValues);
    }

    /*
     *
     * 获取个人资料信息
     * */

    public UserBean getUserInfo(String userName){
        String sql = "SELECT * FROM " +SQLiteHeloer.U_USERINFO+
                " WHERE userName=?";
        Cursor cursor = db.rawQuery(sql,new String[]{userName});
        UserBean bean = null;
        while (cursor.moveToNext()){
            bean = new UserBean();
            bean.userName = cursor.getString(cursor.getColumnIndex("userName"));
            bean.nikeName = cursor.getString(cursor.getColumnIndex("nickName"));
            bean.sex = cursor.getString(cursor.getColumnIndex("sex"));
            bean.signatur = cursor.getString(cursor.getColumnIndex("signature"));
        }
        cursor.close();
        return bean;
    }

    /*
     *
     * 修改个人资料*/

    public void updateUserInfo (String key,String value,String userName){
        ContentValues contentValues = new ContentValues();
        contentValues.put(key, value);
        db.update(SQLiteHeloer.U_USERINFO,contentValues, "userName=?",new String[]{userName});
    }





}
